home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / SlideShow.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  8.7 KB  |  313 lines

  1. package symantec.itools.multimedia;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.awt.Panel;
  7. import java.net.MalformedURLException;
  8. import java.util.Vector;
  9. import java.awt.Image;
  10. import java.net.URL;
  11.  
  12.  
  13. /**
  14.  * This is a basic graphical image "slide show" component. Displays a series of images
  15.  * with descriptive text.
  16.  * @version 1.0, Nov 26, 1996
  17.  * @author Symantec
  18.  */
  19.  
  20. public class SlideShow
  21.     extends Panel
  22. {
  23.     private int imageIndex;
  24.     private ImageViewer imageViewer;
  25.  
  26.     private Vector urlList     = new Vector();
  27.     private Vector images      = new Vector();
  28.     private Vector descriptions = new Vector();
  29.  
  30.     /**
  31.      * Constructs a new SlideShow.
  32.      */
  33.     public SlideShow()
  34.     {
  35.         imageViewer = new ImageViewer();
  36.         imageIndex = 0;
  37.     }
  38.  
  39.     /**
  40.      * Display the first image.
  41.      */
  42.     public void display()
  43.     {
  44.         imageIndex = 0;
  45.  
  46.         try
  47.         {
  48.             images.setElementAt(getToolkit().getImage((URL)urlList.elementAt(0)), imageIndex);
  49.             imageViewer.setImage((Image)images.elementAt(0));
  50.             add(imageViewer);
  51.             repaint();
  52.             validate();
  53.         }
  54.         catch (Exception e)
  55.         {
  56.             System.err.println("Error in SlideShow: Couldn't get image " + ((URL)urlList.elementAt(imageIndex)).toString());
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * Return the number of images in the slide show set.
  62.      */
  63.     public int getNumberOfImages()
  64.     {
  65.         return urlList.size();
  66.     }
  67.  
  68.     /**
  69.      * Return the current image index being displayed.
  70.      */
  71.     public int getCurrentImageIndex()
  72.     {
  73.         return imageIndex;
  74.     }
  75.  
  76.     /**
  77.      * Add an image URL and associated description to the slide
  78.      * show image set.
  79.      * @param url URL of image file
  80.      * @param description description of image
  81.      * @return int - index of added image in slide show set
  82.      */
  83.     public int addImageAndDescription(URL url, String description)
  84.     {
  85.         urlList.addElement(url);
  86.         descriptions.addElement(description);
  87.         images.addElement(null);
  88.  
  89.         int index = urlList.size() - 1;
  90.         if (index == 0)
  91.             display();
  92.  
  93.         return index;
  94.     }
  95.  
  96.     /**
  97.      * Return the URL of the image at the given index.
  98.      * @param index index of image to retrieve URL of
  99.      * @return URL - URL of image at given index
  100.      */
  101.     public URL getURL(int index)
  102.     {
  103.         return (URL)urlList.elementAt(index);
  104.     }
  105.  
  106.     /**
  107.      * Return the description of the image at the given index.
  108.      * @param index index of image to retrieve description of
  109.      * @return String - description of image at given index
  110.      * @see #setDescription
  111.      */
  112.     public String getDescription(int index)
  113.     {
  114.         return (String)descriptions.elementAt(index);
  115.     }
  116.  
  117.     /**
  118.      * Set the description of the image at the given index.
  119.      * @param index index of description to set
  120.      * @param str description string
  121.      * @see #getDescription
  122.      */
  123.     public void setDescription(int index, String str)
  124.     {
  125.         descriptions.setElementAt(str, index);
  126.     }
  127.  
  128.     /**
  129.      * Query if displaying last image in slide show set.
  130.      * @return boolean - true if displaying last image; false
  131.      * otherwise
  132.      * @see #isAtFirstImage
  133.      */
  134.     public boolean isAtLastImage()
  135.     {
  136.         return imageIndex == (urlList.size() - 1);
  137.     }
  138.  
  139.     /**
  140.      * Query if displaying first image in slide show set.
  141.      * @return boolean - true if displaying first image; false
  142.      * otherwise
  143.      * @see #isAtLastImage
  144.      */
  145.     public boolean isAtFirstImage()
  146.     {
  147.         return imageIndex == 0;
  148.     }
  149.  
  150.     /**
  151.      * Display the next image in the slide show set.
  152.      * @see #previousImage
  153.      */
  154.     public int nextImage()
  155.     {
  156.         //try
  157.         {
  158.             if (! isAtLastImage())
  159.             {
  160.                 int rv;
  161.                 ++imageIndex;
  162.                 rv = setImage(imageIndex);
  163.                 getParent().deliverEvent(new Event(this, Event.ACTION_EVENT, new Integer(imageIndex)));
  164.                 return rv;
  165.             }
  166.         }
  167.         /*
  168.         catch (MalformedURLException e)
  169.         {
  170.             System.out.println("Error getting image");
  171.         }
  172.         */
  173.         return imageIndex;
  174.     }
  175.  
  176.     /**
  177.      * Display the previous image in the slide show set.
  178.      * @see #nextImage
  179.      */
  180.     public int previousImage()
  181.     {
  182.         //try
  183.         {
  184.             if (! isAtFirstImage())
  185.             {
  186.                 int rv;
  187.                 --imageIndex;
  188.                 rv = setImage(imageIndex);
  189.                 getParent().deliverEvent(new Event(this, Event.ACTION_EVENT, new Integer(imageIndex)));
  190.                 return rv;
  191.             }
  192.         }
  193.         /*
  194.         catch (MalformedURLException e)
  195.         {
  196.             System.out.println("Error getting image");
  197.         }
  198.         */
  199.         return imageIndex;
  200.     }
  201.  
  202.     /**
  203.      * Display the image at the given index.
  204.      * @param index index of the image to display
  205.      * @param str description string
  206.      * @return int - image index being displayed
  207.      */
  208.     public int setImage(int index)
  209.         //throws MalformedURLException
  210.     {
  211.         if (index >= 0 && index < urlList.size())
  212.         {
  213.             // Load the new image if we do not have it.
  214.             try
  215.             {
  216.                 imageIndex = index;
  217.                 if (images.elementAt(imageIndex) == null)
  218.                     images.setElementAt(getToolkit().getImage((URL)urlList.elementAt(imageIndex)), imageIndex);
  219.             }
  220.             catch (Exception e)
  221.             {
  222.                 //throw new MalformedURLException("Error loading image");
  223.                 System.err.println("Error in SlideShow: Couldn't get image " + ((URL)urlList.elementAt(imageIndex)).toString());
  224.             }
  225.             imageViewer.setImage((Image)images.elementAt(imageIndex));
  226.         }
  227.  
  228.         repaint();
  229.  
  230.         return imageIndex;
  231.     }
  232.  
  233.     /**
  234.      * Returns the recommended dimensions to properly display this component.
  235.      * This is a standard Java AWT method which gets called to determine
  236.      * the recommended size of this component.
  237.      *
  238.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  239.      *          If an image has been loaded, the height and width of the image
  240.      *          is returned.
  241.      * @see java.awt.Container#minimumSize
  242.      */
  243.     public Dimension preferredSize()
  244.     {
  245.         Dimension d;
  246.         d = imageViewer.preferredSize();
  247.  
  248.         return (new Dimension(d.width, d.height));
  249.     }
  250.  
  251.     /**
  252.      * Redraws this component.
  253.      * This is a standard Java AWT method which gets called to redraw this
  254.      * component. It results in a call to update() as soon as possible.
  255.      *
  256.      * @see #update
  257.      * @see java.awt.Component#paint
  258.      */
  259.     public void repaint()
  260.     {
  261.         super.repaint();
  262.         imageViewer.repaint();
  263.     }
  264.  
  265.     /**
  266.      * Handles redrawing of this component on the screen.
  267.      * This is a standard Java AWT method which gets called by the Java
  268.      * AWT (repaint()) to handle repainting this component on the screen.
  269.      * The graphics context clipping region is set to the bounding rectangle
  270.      * of this component and its <0,0> coordinate is this component's
  271.      * top-left corner.
  272.      * Typically this method paints the background color to clear the
  273.      * component's drawing space, sets graphics context to be the foreground
  274.      * color, and then calls paint() to draw the component.
  275.      *
  276.      * It is overridden here to reduce flicker by eliminating the uneeded
  277.      * clearing of the background.
  278.      *
  279.      * @param g the graphics context
  280.      * @see #repaint
  281.      * @see java.awt.Component#paint
  282.      */
  283.     public void update(Graphics g)
  284.     {
  285.         paint(g);
  286.     }
  287.  
  288.     /**
  289.      * Reset slide show and add first image.
  290.      *
  291.      * @param url url of first slide image, if null slide show is reinitialized
  292.      */
  293.     public void setFirstImage(URL url)
  294.     {
  295.         if (url == null) {
  296.             imageViewer.setImage(null);
  297.             imageIndex = 0;
  298.             urlList = new Vector();
  299.             images = new Vector();
  300.             descriptions = new Vector();
  301.         } else {
  302.             if (urlList.size() != 0) {
  303.                 urlList.setElementAt(url, 0);
  304.                 descriptions.setElementAt("", 0);
  305.                 images.setElementAt(null, 0);
  306.                 setImage(0);
  307.             } else {
  308.                 addImageAndDescription(url, "");
  309.             }
  310.         }
  311.     }
  312. }
  313.